package com.systop.asset.controller;

import com.systop.asset.entity.MaterialDisposal;
import com.systop.asset.entity.RestResponse;
import com.systop.asset.service.IMaterialDisposalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>ClassName : MaterialDisposalController</p>
 * <p>Description : 物资处置控制器</p>
 *
 * @author 孙伟光
 * @version 1.0
 * @date 2025/7/7 16:28
 */
@RestController
@RequestMapping("/system/materialDisposal")
public class MaterialDisposalController {

    @Autowired
    private IMaterialDisposalService materialDisposalService;

    /**
     * 根据ID获取物资处置信息
     *
     * @param id 物资处置记录ID，不能为空
     * @return 包含物资处置信息的响应对象
     *         <p>成功时返回状态码200和物资处置数据</p>
     *         <p>失败时返回状态码400(参数错误)或500(服务器内部错误)及错误信息</p>
     */
    @GetMapping("/{id}")
    public RestResponse<MaterialDisposal> getById(@PathVariable Long id) {
        // 参数校验：确保ID不为空
        if (id == null) {
            return RestResponse.failure(400, "物资处置ID不能为空");
        }
        try {
            // 调用服务层查询物资处置信息
            MaterialDisposal result = materialDisposalService.selectMaterialDisposalById(id);
            // 返回查询结果
            return RestResponse.success(result);
        } catch (Exception e) {
            // 捕获并处理异常，返回错误信息
            return RestResponse.failure(500, "查询物资处置信息失败: " + e.getMessage());
        }
    }

    @GetMapping("/detail/{id}")
    public RestResponse<MaterialDisposal> getDetailById(@PathVariable Long id) {
        if (id == null) {
            return RestResponse.failure(400, "物资处置ID不能为空");
        }
        try {
            MaterialDisposal result = materialDisposalService.getMaterialDisposalDetailById(id);
            return RestResponse.success(result);
        } catch (Exception e) {
            return RestResponse.failure(500, "查询物资处置详情失败: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    public RestResponse<List<MaterialDisposal>> getList(MaterialDisposal materialDisposal) {
        try {
            List<MaterialDisposal> result = materialDisposalService.selectMaterialDisposalList(materialDisposal);
            return RestResponse.success(result);
        } catch (Exception e) {
            return RestResponse.failure(500, "查询物资处置列表失败: " + e.getMessage());
        }
    }

    @PostMapping
    public RestResponse<Long> add(@RequestBody MaterialDisposal materialDisposal) {
        if (materialDisposal == null) {
            return RestResponse.failure(400, "物资处置信息不能为空");
        }
        if (materialDisposal.getMaterialDisposalDetails() == null ||
            materialDisposal.getMaterialDisposalDetails().isEmpty()) {
            return RestResponse.failure(400, "物资处置明细不能为空");
        }
        try {
            int result = materialDisposalService.insertMaterialDisposal(materialDisposal);
            return result > 0 ? RestResponse.success(materialDisposal.getId()) :
                              RestResponse.failure(500, "新增物资处置失败");
        } catch (Exception e) {
            return RestResponse.failure(500, "新增物资处置失败: " + e.getMessage());
        }
    }

//    @PutMapping
//    public RestResponse<Void> update(@RequestBody MaterialDisposal materialDisposal) {
//        if (materialDisposal == null || materialDisposal.getId() == null) {
//            return RestResponse.failure(400, "物资处置信息不能为空");
//        }
//        if (materialDisposal.getMaterialDisposalDetails() == null ||
//            materialDisposal.getMaterialDisposalDetails().isEmpty()) {
//            return RestResponse.failure(400, "物资处置明细不能为空");
//        }
//        try {
//            int result = materialDisposalService.updateMaterialDisposal(materialDisposal);
//            return result > 0 ? RestResponse.success() :
//                              RestResponse.failure(500, "更新物资处置失败");
//        } catch (Exception e) {
//            return RestResponse.failure(500, "更新物资处置失败: " + e.getMessage());
//        }
//    }

    @DeleteMapping("/{id}")
    public RestResponse<Void> delete(@PathVariable Long id) {
        if (id == null) {
            return RestResponse.failure(400, "物资处置ID不能为空");
        }
        try {
            int result = materialDisposalService.deleteMaterialDisposalById(id);
            return result > 0 ? RestResponse.success() :
                              RestResponse.failure(500, "删除物资处置失败");
        } catch (Exception e) {
            return RestResponse.failure(500, "删除物资处置失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/batch")
    public RestResponse<Void> batchDelete(@RequestBody Long[] ids) {
        if (ids == null || ids.length == 0) {
            return RestResponse.failure(400, "物资处置ID列表不能为空");
        }
        try {
            int result = materialDisposalService.deleteMaterialDisposalByIds(ids);
            return result > 0 ? RestResponse.success() :
                              RestResponse.failure(500, "批量删除物资处置失败");
        } catch (Exception e) {
            return RestResponse.failure(500, "批量删除物资处置失败: " + e.getMessage());
        }
    }
}
